#coding:utf-8
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext

from lovewith.views import site_info
from lovewith.share.views.modules.attach_flag import AttachFlagApi
from lovewith.promote.views.modules.goods_tag import GoodsAttachTagApi
from lovewith.website.views.modules.category import CategoryApi


#获取全部商品
def get_goods(request, tag=None, page_no=1):
    if not page_no:
        page_no = 1

    goods_tag = GoodsAttachTagApi.get_all()
    goods_data = AttachFlagApi.get_by_tag(tag=tag, page_no=int(page_no))

    if not tag:
        current_tag = 'all'
    else:
        current_tag = tag

    return render_to_response('share/goods.jinja', {
        'goods_tag': goods_tag,
        'current_tag': current_tag,
        'share_data': goods_data,
        'total': goods_data.get('total'),
        'current_page': page_no,
        'total_page': goods_data.get('total_page')
    }, context_instance=RequestContext(request, processors=[site_info]))


#获取类目商品
def get_goods_by_cate(request, cate_id, page_no=1):
    if not page_no:
        page_no = 1

    cate_data = CategoryApi.check_data(cate_id)
    if not cate_data:
        return HttpResponseRedirect('/share/goods/all/')

    goods_tag = GoodsAttachTagApi.get_all()
    goods_data = AttachFlagApi.get_by_cate(cate_id=cate_id, page_no=int(page_no))

    return render_to_response('share/goods.jinja', {
        'goods_tag': goods_tag,
        'current_cate': cate_data,
        'share_data': goods_data,
        'total': goods_data.get('total'),
        'current_page': page_no,
        'total_page': goods_data.get('total_page')
    }, context_instance=RequestContext(request, processors=[site_info]))
